import 'package:flutter/material.dart';
import 'package:flutter_business_2003a/bean/cart_info_bean_result.dart';
import 'package:flutter_business_2003a/provider/cart_provider.dart';
import 'package:flutter_screenutil/screenutil.dart';
import 'package:provider/provider.dart';

class CartCount extends StatelessWidget {
  final CartInfoBeanResult item;

  const CartCount({Key? key, required this.item}) : super(key: key);

  //减少的按钮
  Widget _reduceBtn(context) {
    return InkWell(
      onTap: () {
        Provider.of<CartProvider>(context,listen: false).addOrReduceAction(item, 'reduce');
      },
      child: Container(
        width: ScreenUtil().setWidth(45).toDouble(),
        height: ScreenUtil().setHeight(45).toDouble(),
        alignment: Alignment.center,
        decoration: BoxDecoration(
          color: item.count! > 1 ? Colors.white : Colors.black12,
          border: const Border(
              right: BorderSide(width: 1.0, color: Colors.black12)),
        ),
        child: item.count! > 1 ? const Text('-') : const Text(' '),
      ),
    );
  }

  //增加的按钮
  Widget _addBtn(context) {
    return InkWell(
      onTap: () {
        Provider.of<CartProvider>(context,listen: false).addOrReduceAction(item, 'add');
      },
      child: Container(
        width: ScreenUtil().setWidth(45).toDouble(),
        height: ScreenUtil().setHeight(45).toDouble(),
        alignment: Alignment.center,
        decoration: const BoxDecoration(
          color: Colors.white,
          border: Border(left: BorderSide(width: 1.0, color: Colors.black12)),
        ),
        child: const Text('+'),
      ),
    );
  }

  Widget _countArea() {
    return Container(
      width: ScreenUtil().setWidth(70).toDouble(),
      height: ScreenUtil().setHeight(45).toDouble(),
      alignment: Alignment.center,
      color: Colors.white,
      child: Text('${item.count}'),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: ScreenUtil().setWidth(165).toDouble(),
      margin: const EdgeInsets.only(top: 5.0),
      decoration:
          BoxDecoration(border: Border.all(width: 1.0, color: Colors.black12)),
      child: Row(
        children: [_reduceBtn(context), _countArea(), _addBtn(context)],
      ),
    );
  }
}
